package com.ebupt.migu.music.global.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ebupt.migu.globalpopup.entity.ConditionData;
import com.ebupt.migu.globalpopup.enums.FropertyEnum;
import com.ebupt.migu.globalpopup.entity.Identifier;
import com.ebupt.migu.globalpopup.entity.Property;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.global.mapper.ConditionDataMapper;
import com.ebupt.migu.music.global.mapper.IdentifierMapper;
import com.ebupt.migu.music.global.mapper.PropertyMapper;
import com.ebupt.migu.music.global.service.ConditionRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ConditionRelationServiceImpl  implements ConditionRelationService {

    @Autowired
    private IdentifierMapper identifierMapper;

    @Autowired
    private PropertyMapper propertyMapper;

    @Autowired
    private ConditionDataMapper conditionDataMapper;

    @Override
    public ResultObject getConditions() {
        ResultObject resultObject = new ResultObject();

        List<Identifier> identifiers = identifierMapper.selectList(new QueryWrapper<>());
        List<Property> properties = propertyMapper.selectList(new QueryWrapper<>());
        List<ConditionData> conditionData = conditionDataMapper.selectList(new QueryWrapper<>());
        Map<Integer, List<Property>> PropertyListMap = properties.stream().collect(Collectors.groupingBy(Property::getPropertyCode));
        Map<Integer, List<ConditionData>> ConditionDataListMap = conditionData.stream().collect(Collectors.groupingBy(ConditionData::getDataType));

        ArrayList<HashMap> list = new ArrayList<>();
        for (Map.Entry<Integer, List<Property>> listEntry : PropertyListMap.entrySet()) {
            HashMap resultMap = new HashMap<>();
            resultMap.put("PropertyCode", listEntry.getKey());
            resultMap.put("PropertyName", FropertyEnum.getCodeByName(listEntry.getKey()));
            resultMap.put("PropertyKey", FropertyEnum.getCodeByEnName(listEntry.getKey()));
            List<HashMap> categorys = listEntry.getValue().stream().map(property -> {
                HashMap<String, Object> map = new HashMap<>(3);
                map.put("ConditionKey", property.getConditionKey());
                map.put("ConditionValue", property.getConditionValue());
                map.put("ConditionTip", property.getConditionTip());
                map.put("order", property.getPropertyOrder());
                List<ConditionData> conditionDataList = ConditionDataListMap.get(property.getDataType());
                if (conditionDataList != null) {
                    List<HashMap> datas = conditionDataList.stream().map(cd -> {
                        HashMap cdmap = new HashMap<>();
                        cdmap.put("DataKey", cd.getDataKey());
                        cdmap.put("DataValue", cd.getDataValue());
                        return cdmap;
                    }).collect(Collectors.toList());
                    map.put("datas", datas);
                }
                if (property.getRelations() != null) {
                    List<HashMap> relations = Arrays.stream(property.getRelations().split(",")).map(s -> {
                        Identifier idf = identifiers.stream().filter(identifier -> identifier.getRelation().toString().equals(s)).collect(Collectors.toList()).get(0);
                        HashMap imap = new HashMap<>();
                        imap.put("Relation", idf.getRelation());
                        imap.put("RelationValue", idf.getRelationValue());
                        return imap;
                    }).collect(Collectors.toList());
                    map.put("relations", relations);
                }

                return map;
            }).collect(Collectors.toList());
            resultMap.put("categorys", categorys);
            list.add(resultMap);
        }
        Collections.sort(list, (map1, map2) -> {
            List<HashMap> list1= (List<HashMap>) map1.get("categorys");
            Integer value1 = (Integer) list1.get(0).get("order");
            List<HashMap> list2= (List<HashMap>) map2.get("categorys");
            Integer value2 = (Integer) list2.get(0).get("order");
            return value1.compareTo(value2);
        });
        resultObject.setCode("0000");
        resultObject.setMsg("成功");
        resultObject.setData(list);
        return resultObject;
    }

}

